﻿namespace PowerSong.UserInterface
    {
    using System;
    using System.Linq;
    using System.Windows;
    using PowerSong.SongDatabase;

    /// <summary>
    /// Interaction logic for DeleteCategoryWindow.xaml
    /// </summary>
    public partial class DeleteCategoryWindow : Window
        {

        private Category category = null;

        public DeleteCategoryWindow(Category category)
            {

            InitializeComponent();
            ReassignSongsRadioButton.IsChecked = true;
            this.category = category;
            this.NameTextBox.Text = category.Name;

            // Show songs that depend exclusively on this category
            SongsListBox.Items.Clear();
            foreach (var song in State.Database.Categories.GetSongs(category).OrderBy(song => song.Title))
                {
                if (song.Categories.Count == 1) SongsListBox.Items.Add(song);
                }

            // Load a list of other available categories
            foreach (var otherCategory in State.Database.Categories.GetAllCategories().OrderBy(c => c.Name))
                {
                if (otherCategory != category) OtherCategoriesComboBox.Items.Add(otherCategory);
                }

            // Are there any alternative categories?
            if (OtherCategoriesComboBox.Items.Count < 1)
                {
                OtherCategoriesComboBox.IsEnabled = false;
                ReassignSongsRadioButton.IsEnabled = false;
                DeleteSongsRadioButton.IsChecked = true;
                }

            }

        private void OKButton_Click(object sender, RoutedEventArgs e)
            {

            try
                {

                // What did the user choose to do?
                if (ReassignSongsRadioButton.IsChecked == true)
                    {

                    // The user must have selected an alternative category
                    if (OtherCategoriesComboBox.SelectedItem == null)
                        {
                        throw new Exception("Please select an alternative category to assign the songs to, because every song must be assigned at least one category.");
                        }

                    // Reassign each song in the list to the selected category
                    foreach (Song song in SongsListBox.Items)
                        {
                        song.Categories.Remove(category);
                        song.Categories.Add((Category)OtherCategoriesComboBox.SelectedItem);
                        State.Database.Songs.Write(song);
                        }

                    // Finally, delete the category
                    State.Database.Categories.Delete(category);

                    // Close the window
                    this.DialogResult = true;

                    }
                else
                    {

                    // Confirm that the user wishes to delete all the songs assigned the category
                    if (Utilities.Confirm("Delete Songs", "Are you sure you wish to delete all of the songs in the list, as well as the category itself?"))
                        {

                        // Delete all the songs
                        foreach (Song song in SongsListBox.Items)
                            {
                            State.Database.Songs.Delete(song);
                            }

                        // Delete the category
                        State.Database.Categories.Delete(category);

                        // Close the window
                        this.DialogResult = true;

                        }

                    }

                }
            catch (Exception ex)
                {
                Utilities.ShowErrorMessage("Cannot Delete Category", ex.Message);
                }

            }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
            this.DialogResult = false;
            }

        private void RadioButtonSelected(object sender, RoutedEventArgs e)
            {
            OtherCategoriesComboBox.IsEnabled = ReassignSongsRadioButton.IsChecked == true;
            }

        }

    }
